package com.ruoyi.flowable.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.flowable.domain.ProcInstVo;
import com.ruoyi.flowable.service.IProcInstService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/flowable/procInst")
public class ProcInstController extends BaseController {

    @Resource
    private IProcInstService procInstService;

    /**
     * 我的发起列表
     */
    @SaCheckPermission("flowable:procInst:list")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(name = "processName", required = false) String processName) {
        return procInstService.myProcInstList(pageNum, pageSize, processName);
    }

    /**
     * 发起流程
     */
    @SaCheckPermission("flowable:procInst:start")
    @PostMapping("/start/{appVersionId}")
    public R<Void> start(@PathVariable(value = "appVersionId") Long appVersionId,
                         @RequestBody Map<String, Object> form) throws JsonProcessingException {
        procInstService.startProcessInst(appVersionId, form);
        return R.ok("发起成功");
    }


    /**
     * 删除实例信息
     */
    @SaCheckPermission("flowable:procInst:delete")
    @DeleteMapping(value = "/{procInstId}")
    public R<Void> delete(@PathVariable(value = "procInstId") String procInstId) {
        procInstService.delete(procInstId,"删除实例");
        return R.ok();
    }

    @SaCheckPermission("flowable:procInst:exportPdf")
    @PostMapping("/exportPdf")
    public void exportPdf(ProcInstVo procInstVo, HttpServletResponse response) throws Exception {
        procInstService.exportPdf(procInstVo.getProcessInstanceId(), response);
    }


}
